/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import business.CarDetailBusiness;
import java.util.ArrayList;
import model.CarDetails;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Administrator
 */
@ManagedBean
@RequestScoped
public class ListCarDetailsCustomerBean {

    private CarDetailBusiness carDetailBusiness = new CarDetailBusiness();
    private List<CarDetails> listCarDetails;
    private List<Integer> pageIndex;
    private int modelID;
    private int pageID;

    public int getModelID() {
        return modelID;
    }

    public void setModelID(int modelID) {
        this.modelID = modelID;
    }

    public int getPageID() {
        return pageID;
    }

    public void setPageID(int pageID) {
        this.pageID = pageID;
    }

    public List<Integer> getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(List<Integer> pageIndex) {
        this.pageIndex = pageIndex;
    }

    public List<CarDetails> getListCarDetails() {
        return listCarDetails;
    }

    public void setListCarDetails(List<CarDetails> listCarDetails) {
        this.listCarDetails = listCarDetails;
    }

    /** Creates a new instance of ListCarDetailsCustomerBean */
    public ListCarDetailsCustomerBean() {
        if (FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("modelID") != null) {
            modelID = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("modelID").toString());
            pageID = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("pageID").toString());
        } else {
            pageID = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("PageID").toString());
            modelID = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("ModelID").toString());
        }
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        session.setAttribute("PageID", pageID);
        session.setAttribute("ModelID", modelID);

        int index = carDetailBusiness.getRowInListCarByModelID(modelID);
        pageIndex = new ArrayList<Integer>();
        if (index / 3 < 1) {
            pageIndex.add(1);
        } else {
            if (index % 3 == 0) {
                for (int i = 1; i <= index / 3; i++) {
                    pageIndex.add(i);
                }
            } else {
                for (int i = 1; i <= index / 3 + 1; i++) {
                    pageIndex.add(i);
                }
            }
        }
        listCarDetails = carDetailBusiness.ListCarByModelIDAndPage(modelID, pageID);
    }
}
